﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Management;
using System.Windows.Forms;

namespace Helix_Network_Supervisor_3
{
    class XBeeSerialPort
    {
        #region Fields

        private SerialPort XBee = new SerialPort();  //Serial Port object used to communicate with the XBee Pro
        private ApplicationSettings appSettings = new ApplicationSettings();
        private List<string> comPortList = new List<string>();
        private string portName;
        private int baudRate;
        private string panID;

        #endregion Fields

        #region Ctor

        public XBeeSerialPort(ComboBox xBeeComPortComboBox)
        {
            setPortNames(xBeeComPortComboBox);
        }

        #endregion Ctor

        #region Properties

        #endregion Properties

        #region Public Methods

        #endregion Public Methods

        #region Private Methods

        public void setPortNames(object obj)
        {
            /*This method populates a combo box with all of the available serial ports*/

            comPortList.Clear();
            foreach (string str in SerialPort.GetPortNames())
            {
                comPortList.Add(str);
                //((ComboBox)obj).Items.Add(str);
            }

            bool found;

            //Get the “friendly” name for each COM port if available

            foreach (string str in comPortList)
            {
                found = false;

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select Name from Win32_PnpEntity");

                foreach (ManagementObject devices in searcher.Get())
                {
                    string name = devices.GetPropertyValue("Name").ToString();

                    // only add item if the friendly names contains "COM"      
                    // we only want to add COM Ports      
                    if (name.Contains(str))
                    {
                        ((ComboBox)obj).Items.Add(name);
                        //MessageBox.Show(name);
                        found = true;
                    }
                }

                searcher.Dispose();
                if (!found)
                {
                    ((ComboBox)obj).Items.Add(str);
                    //MessageBox.Show(str);
                }
            }
            try
            {
                if (((ComboBox)obj).Items.Count > 0)
                {
                    ((ComboBox)obj).SelectedIndex = 0;
                }
                appSettings.PortName = comPortList[0];
            }
            catch
            {
                appSettings.PortName = "";
            }

        }

        public void savePortName(ComboBox xBeeComPortComboBox)
        {
            try
            {
                int index = xBeeComPortComboBox.SelectedIndex;
                appSettings.PortName = comPortList[index];
            }
            catch
            {
                appSettings.PortName = "";
            }
            MessageBox.Show("COM Port:  " + appSettings.PortName);
        }

        public bool openXBee()
        {
            //Attempt to open the XBee with the current settings; notify the user if failure
            if (XBee.IsOpen == true) XBee.Close();

            try  //Get the COM Port that the XBee is connected to
            {
                portName = appSettings.PortName;

                if (portName == "")
                {
                    MessageBox.Show("Please select the COM Port");
                    return false;
                }

            }
            catch
            {
                MessageBox.Show("Please select the COM Port");
                return false;
            }
            MessageBox.Show("Port Name:  " + portName);

            try  //Get the desired baud rate
            {
                baudRate = appSettings.BaudRate;
            }
            catch
            {
                MessageBox.Show("Please select the baud rate");
                return false;
            }

            XBee.DataBits = 8;
            XBee.StopBits = StopBits.One;
            XBee.Parity = Parity.None;
            XBee.PortName = portName;
            XBee.BaudRate = baudRate;

            try
            {
                XBee.Open();
                return true;
            }
            catch
            {
                MessageBox.Show("Error trying to open XBee port");
                return false;
            }
        }  //End openXBee

        #endregion Private Methods

    }
}
